﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Churchtoolkit.Utils.MVP;
using Churchtoolkit.DataAccess.Repositories;
using Churchtoolkit.Utils.Authentication;
using Churchtoolkit.DataAccess.Entities;
using Churchtoolkit.DataAccess.Common;
using Churchtoolkit.Web.Shell.Views;

namespace Churchtoolkit.Web.Shell.Presenters
{
    public class PersonalInfoPresenter : Presenter<IPersonalInfoView>
    {

        private MembersRepository _MembersRepository;
        private CountriesRepository _CountriesRepository;
        private NationalitiesRepository _NationalitiesRepository;
        private IAuthentication _IAuthentication;
        private MemberPhonesRepository _MemberPhonesRepository;
        private MemberAddressesRepository _MemberAddressesRepository;
       
        public PersonalInfoPresenter(MembersRepository MembersRepository, MemberPhonesRepository MemberPhonesRepository, MemberAddressesRepository MemberAddressesRepository, CountriesRepository CountriesRepository, NationalitiesRepository NationalitiesRepository, IAuthentication IAuthentication)
        {
            _MembersRepository = MembersRepository;
            _MemberPhonesRepository = MemberPhonesRepository;
            _MemberAddressesRepository = MemberAddressesRepository;
            _CountriesRepository = CountriesRepository;
            _NationalitiesRepository = NationalitiesRepository;
            _IAuthentication = IAuthentication;

        }

        public List<Countries> GetCountries()
        {
            EntityResult<Countries> Countries;
            Countries = _CountriesRepository.Find(null);
            List<Countries> lstCountries = new List<Countries>();
            foreach (Countries Country in Countries.Rows)
            {
                lstCountries.Add(new Countries
                {
                    CountryId = Country.CountryId,
                    CountryName = Country.CountryName
                });
            }
            return lstCountries;
        }
        public List<Nationalities> GetNationalities()
        {
            EntityResult<Nationalities> Nationalities;
            Nationalities = _NationalitiesRepository.Find(null);
            List<Nationalities> lstNationalities = new List<Nationalities>();
            foreach (Nationalities Nationality in Nationalities.Rows)
            {
                lstNationalities.Add(new Nationalities
                {
                    NationalityId = Nationality.NationalityId,
                    NationalityName = Nationality.NationalityName
                });
            }
            return lstNationalities;
        }
        public List<MemberPhones> GetMemberPhones()
        {
            EntityResult<MemberPhones> Phones;
            Phones = _MemberPhonesRepository.Find(item => item.MemberId = _IAuthentication.CurrentLogedInMember.Id);
            List<MemberPhones> MemberPhones = new List<MemberPhones>();
            foreach (MemberPhones Phone in Phones.Rows)
            {
                MemberPhones.Add(new MemberPhones { PhoneId = Phone.PhoneId, Phone = Phone.Phone, PhoneType = Phone.PhoneType });
            }
            return MemberPhones;
        }
        public MemberAddresses GetMemberAddresses()
        {
            try
            {
                return _MemberAddressesRepository.Find(item => item.MemberId = _IAuthentication.CurrentLogedInMember.Id).Single();
            }
            catch (Exception)
            {
                return new MemberAddresses();
            }
        }
        public Members GetCurrentMemberPersonalInfo()
        {
            try
            {
                return _MembersRepository.Find(item => item.MemberId = _IAuthentication.CurrentLogedInMember.Id).Single();
            }
            catch (Exception)
            {
                return new Members();
            }
        }


        public void UpdateMemberInfo(Members Members)
        {
            Members.MemberId = _IAuthentication.CurrentLogedInMember.Id;
            _MembersRepository.Update(Members);
        }
        public void AddMemberPhones(MemberPhones[] MemberPhones)
        {
            foreach (MemberPhones phone in MemberPhones)
                phone.MemberId = _IAuthentication.CurrentLogedInMember.Id;

            _MemberPhonesRepository.BulckInsert(MemberPhones);
        }
        public void AddMemberAddresses(MemberAddresses MemberAddresses)
        {
            MemberAddresses.MemberId = _IAuthentication.CurrentLogedInMember.Id;
            _MemberAddressesRepository.Insert(MemberAddresses);
        }
        public void UpdateMemberPhones(MemberPhones[] MemberPhones)
        {
            foreach (MemberPhones phone in MemberPhones)
                phone.MemberId = _IAuthentication.CurrentLogedInMember.Id;

            _MemberPhonesRepository.BulckUpdate(MemberPhones);

        }
        public void UpdateMemberAddresses(MemberAddresses MemberAddresses)
        {
            MemberAddresses.MemberId = _IAuthentication.CurrentLogedInMember.Id;
            _MemberAddressesRepository.Update(MemberAddresses);

        }
    }
}
